package com.ztools.pc.controller.information;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ztools.common.model.pc.information.InformationPoint;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.common.Result;
import com.ztools.common.util.common.enumUnit.PointOperationEnum;
import com.ztools.pc.service.information.InformationPointService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author seeTools
 * @since 2023-06-02
 */
@RestController
@RequestMapping("/information/point")
public class InformationPointController {

    @Resource
    private InformationPointService informationPointService;

    @PostMapping("/list")
    public Result findList(@RequestBody PageUtil<InformationPoint> pageUtil) {
        Page<InformationPoint> list = informationPointService.findList(pageUtil);
        return list != null ? Result.success("查询成功").data(list) : Result.error();
    }

    @RequestMapping("/findByObject")
    public Result findByObject(@RequestBody InformationPoint informationPoint) {
        List<InformationPoint> list = informationPointService.findByObject(informationPoint);
        return list != null ? Result.success("查询成功").data(list) : Result.error();
    }

    @RequestMapping("/update")
    public Result update(@RequestBody InformationPoint informationPoint) {
        boolean b = informationPointService.update(informationPoint);
        return b ? Result.success("修改成功") : Result.error();
    }

    @RequestMapping("/delete")
    public Result delete(@RequestBody InformationPoint informationPoint) {
        boolean b = informationPointService.delete(informationPoint);
        return b ? Result.success("删除成功") : Result.error();
    }

    @RequestMapping("/add")
    public Result add(@RequestBody InformationPoint informationPoint) {
        boolean b = informationPointService.save(informationPoint);
        return b ? Result.success("添加成功") : Result.error();
    }

    @RequestMapping("/increase")
    public Result findById(@RequestBody InformationPoint informationPoint) {
        boolean b = informationPointService.increase(informationPoint);
        return b ? Result.success("积分增加成功") : Result.error();
    }

    @RequestMapping("/changePoint")
    public Result addPoint(@RequestBody InformationPoint informationPoint) {
        boolean b = false;
        if (PointOperationEnum.increase.getOperationType().equals(informationPoint.getOperationType())) {
            b = informationPointService.increase(informationPoint);
        } else {
            b = informationPointService.reduce(informationPoint);
        }
        return b ? Result.success("积分调整成功") : Result.error();
    }

    @RequestMapping("/export")
    public void excel(HttpServletResponse response, @RequestBody InformationPoint informationPoint) {
        informationPointService.exportExcel(response, informationPoint);
    }

}
